﻿using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using YJKArchMethodLibrary.Previewer;
using YJKArchMethodLibrary.Utils;

namespace ColumnModuleAndEdit.ColumnModule
{
	public class ColumnProfilePlotter
	{
		public List<FormCurveInfo> Curves
		{
			get
			{
				return this.m_Curves;
			}
		}

		public List<FormTextNoteInfo> Texts
		{
			get
			{
				return this.m_Texts;
			}
		}

		public ColumnProfilePlotter(List<Curve> profileEdgeLoop, List<KeyValuePair<string, Curve>> showCenterCurve, List<KeyValuePair<string, Curve>> hideCenterCurve, double minWallThickness)
		{
			this.m_ProfileEdgeLoop = profileEdgeLoop;
			this.m_ShowCenterCurve = showCenterCurve;
			this.m_HideCenterCurve = hideCenterCurve;
			this.m_MinWallThickness = minWallThickness;
		}

		public void Draw(PenColor profileColor = PenColor.PC_Gray, PenColor dimensionColor = PenColor.PC_Black, PenColor textColor = PenColor.PC_Black)
		{
			float lineWidth = 2f;
			foreach (Curve curve in this.m_ProfileEdgeLoop)
			{
				FormCurveInfo item = new FormCurveInfo(curve, profileColor, lineWidth, true);
				this.m_Curves.Add(item);
			}
			double num = this.m_MinWallThickness / 2.0;
			float num2 = 1f;
			foreach (KeyValuePair<string, Curve> keyValuePair in this.m_ShowCenterCurve)
			{
				FormCurveInfo formCurveInfo = new FormCurveInfo(keyValuePair.Value, dimensionColor, num2, true);
				this.m_Curves.Add(formCurveInfo);
				this.DrawDimension(keyValuePair.Value, dimensionColor, num2);
				XYZ center = formCurveInfo.MidPoint();
				double boundingBoxWidth = num * (double)this.GetLength(keyValuePair.Key);
				FormTextNoteInfo item2 = new FormTextNoteInfo(keyValuePair.Key, textColor, num * 0.8, center, boundingBoxWidth, num);
				this.m_Texts.Add(item2);
			}
			foreach (KeyValuePair<string, Curve> keyValuePair2 in this.m_HideCenterCurve)
			{
				FormCurveInfo formCurveInfo2 = new FormCurveInfo(keyValuePair2.Value, dimensionColor, num2, true);
				XYZ center2 = formCurveInfo2.MidPoint();
				double boundingBoxWidth2 = num * (double)this.GetLength(keyValuePair2.Key);
				FormTextNoteInfo item3 = new FormTextNoteInfo(keyValuePair2.Key, textColor, num * 0.8, center2, boundingBoxWidth2, num);
				this.m_Texts.Add(item3);
			}
		}

		public void DrawBlock()
		{
			PenColor color = PenColor.PC_Gray;
			float lineWidth = 2f;
			foreach (Curve curve in this.m_ProfileEdgeLoop)
			{
				FormCurveInfo item = new FormCurveInfo(curve, color, lineWidth, true);
				this.m_Curves.Add(item);
			}
		}

		private void DrawDimension(Curve curve, PenColor dimensionColor, float dimensionLineWidth)
		{
			if (curve.GetType() == typeof(Line))
			{
				this.DrawLineDimension(curve as Line, dimensionColor, dimensionLineWidth);
				return;
			}
			if (curve.GetType() == typeof(Arc))
			{
				this.DrawArcDimension(curve as Arc, dimensionColor, dimensionLineWidth);
			}
		}

		private void DrawLineDimension(Line line, PenColor dimensionColor, float dimensionLineWidth)
		{
			XYZ hyendPoint = line.GetEndPoint(0);
			XYZ hyendPoint2 = line.GetEndPoint(1);
			double num = 10.0;
			XYZ pt = (hyendPoint2 - hyendPoint).Normalize() * num;
			XYZ xyz = Geometry.RotateTo(pt, Math.PI*.5, XYZ.BasisZ);
			XYZ dir = xyz.Negate();
			XYZ xyz2 = Geometry.RotateTo(pt, 1.0471975511965976, XYZ.BasisZ) / 2.0;
			XYZ dir2 = xyz2.Negate();
			this.AddDimensionLine(hyendPoint, xyz, dimensionColor, dimensionLineWidth);
			this.AddDimensionLine(hyendPoint, dir, dimensionColor, dimensionLineWidth);
			this.AddDimensionLine(hyendPoint, xyz2, dimensionColor, dimensionLineWidth);
			this.AddDimensionLine(hyendPoint, dir2, dimensionColor, dimensionLineWidth);
			this.AddDimensionLine(hyendPoint2, xyz, dimensionColor, dimensionLineWidth);
			this.AddDimensionLine(hyendPoint2, dir, dimensionColor, dimensionLineWidth);
			this.AddDimensionLine(hyendPoint2, xyz2, dimensionColor, dimensionLineWidth);
			this.AddDimensionLine(hyendPoint2, dir2, dimensionColor, dimensionLineWidth);
		}

		private void DrawArcDimension(Arc arc, PenColor dimensionColor, float dimensionLineWidth)
		{
			XYZ hyendPoint = arc.GetEndPoint(0);
			XYZ hyendPoint2 = arc.GetEndPoint(1);
			XYZ center = arc.Center;
			double num = 10.0;
			XYZ xyz = (hyendPoint - center).Normalize() * num;
			XYZ dir = xyz.Negate();
			XYZ xyz2 = Geometry.RotateTo(xyz, -0.52359877559829882, XYZ.BasisZ) / 2.0;
			XYZ dir2 = xyz2.Negate();
			this.AddDimensionLine(hyendPoint, xyz, dimensionColor, dimensionLineWidth);
			this.AddDimensionLine(hyendPoint, dir, dimensionColor, dimensionLineWidth);
			this.AddDimensionLine(hyendPoint, xyz2, dimensionColor, dimensionLineWidth);
			this.AddDimensionLine(hyendPoint, dir2, dimensionColor, dimensionLineWidth);
			XYZ xyz3 = (hyendPoint2 - center).Normalize() * num;
			XYZ dir3 = xyz3.Negate();
			XYZ xyz4 = Geometry.RotateTo(xyz3, -0.52359877559829882, XYZ.BasisZ) / 2.0;
			XYZ dir4 = xyz4.Negate();
			this.AddDimensionLine(hyendPoint2, xyz3, dimensionColor, dimensionLineWidth);
			this.AddDimensionLine(hyendPoint2, dir3, dimensionColor, dimensionLineWidth);
			this.AddDimensionLine(hyendPoint2, xyz4, dimensionColor, dimensionLineWidth);
			this.AddDimensionLine(hyendPoint2, dir4, dimensionColor, dimensionLineWidth);
		}

		private void AddDimensionLine(XYZ ptOrigin, XYZ dir, PenColor dimensionColor, float dimensionLineWidth)
		{
			Line curve = YJKLineEx.YJKGetBound(ptOrigin, ptOrigin + dir);
			FormCurveInfo item = new FormCurveInfo(curve, dimensionColor, dimensionLineWidth, false);
			this.m_Curves.Add(item);
		}

		private int GetLength(string str)
		{
			if (str.Length == 0)
			{
				return 0;
			}
			ASCIIEncoding asciiencoding = new ASCIIEncoding();
			int num = 0;
			byte[] bytes = asciiencoding.GetBytes(str);
			for (int i = 0; i < bytes.Length; i++)
			{
				if (bytes[i] == 63)
				{
					num += 2;
				}
				else
				{
					num++;
				}
			}
			return num;
		}

		private List<Curve> m_ProfileEdgeLoop;

		private List<KeyValuePair<string, Curve>> m_ShowCenterCurve;

		private List<KeyValuePair<string, Curve>> m_HideCenterCurve;

		private List<FormCurveInfo> m_Curves = new List<FormCurveInfo>();

		private List<FormTextNoteInfo> m_Texts = new List<FormTextNoteInfo>();

		private double m_MinWallThickness;
	}
}
